<?php
namespace Home\Controller;
use Think\Controller;
class CronsController extends Controller
{
  /**
   * 扫描过期的，进行退单
   * 每2个小时处理一次
   */
    public function cronentrsut(){
       $cron=M('CronLog');
       $crdata['typ']=1;
       $crdata['creatdate']=date('y-m-d H:m:s',time());
       $crdata['desc']='开始执行扫描过期委单-进行自动撤单';
       $cron->add($crdata);
            $cond['status'] = 1;
            $cond['expire_date'] = array('elt',date("Y-m-d").' 00:00:00');
            $list=M('user_entrust')->field('id,trans_type,uid,trans_mode,domain_typeid,unit_price,surplus')->where($cond)->select();
            if(empty($list)){
            }else{
                foreach ($list as $result){
                    //卖出域名
                    if($result['trans_type']==2){
                        $updcond="update dd_user_domains set trans_status=1 where uid=".$result['uid']." and domain_typeid=".$result['domain_typeid']." and check_status=3 and trans_status=2 and is_mortage=".$result['trans_mode']." order by createdate limit ".$result['surplus'];
                        $Upao = M();
                        $upnum = $Upao->execute($updcond);
                    }  else {
                        //全款购买-xuyao 退回冻结保证金
                        if($result['trans_mode']==1){
                            $dongjie=($result['surplus'])*($result['unit_price']);
                            M('UserAssets')->where('uid='.$result['uid'])->setDec('locked_money',$dongjie);
                            M('UserAssets')->where('uid='.$result['uid'])->setInc('notpresent_money',$dongjie);
                            M('UserAssets')->where('uid='.$result['uid'])->setInc('avail_money',$dongjie);
                        }
                    }
                    $result = M('user_entrust')->where($cond)->setField('status',2);
                }
          }
          exit();
    }
    
    
    
    /**
     * 扫描过期的借款标，
     * 每3个小时处理一次
     */
    public function cronenloadinfo(){
        $cron=M('CronLog');
        $crdata['typ']=1;
        $crdata['creatdate']=date('y-m-d H:m:s',time());
        $crdata['desc']='开始执行扫描过期的借款标-退款给投资人';
        $cron->add($crdata);
        $cond['status'] = 1;
        $cond['overtime'] = array('elt',date("Y-m-d").' 00:00:00');
        $list=M('LoaderInfo')->field('tovalue,cred_code')->where($cond)->select();
        if(empty($list)){
        }else{
            foreach ($list as $load){
               $flowList=M('CredFlow')->field('id,investuid,money')->where("cred_code='".$load['cred_code']."'")->select();
               if(!empty($flowList)){
                   foreach ($flowList as $flow){
                       M('UserAssets')->where('uid='.$flow['investuid'])->setInc('creditmoney',$flow['money']);
                       M('CredFlow')->where("id=".$flow['id'])->setField(array('status'=>5));
                   }
               }
               M('LoaderInfo')->where("cred_code='".$load['cred_code']."'")->setField(array('status'=>3));
            }
        }
       exit();
    }
    
    
    /**
     * 扫描所有即将逾期的借款
     * 每3个小时处理一次
     * //借款金额*2%/30天*逾期天数*1.4倍
     */
    public function cronenyqflow(){
        $cron=M('CronLog');
        $crdata['typ']=1;
        $crdata['creatdate']=date('y-m-d H:m:s',time());
        $crdata['desc']='开始执行扫描即将逾期的借款-已经逾期超过30天的修改抵押中的域名为平台暂扣售卖';
        $cron->add($crdata);
        $cond['is_yq'] = array('in','1,2');//查询出逾期的，和未逾期的
        $cond['repaydate'] = array('lt',date("Y-m-d").' 00:00:00');
        $start=0;
        while (true){
            $repaylist=M('RepaymentFlow')->field('id,cred_flowid,is_yq,repaydate,cred_code')->where($cond)->order('repaydate desc')->limit($start,100)->select();
            if(empty($repaylist)){
                break;
            }else{
                foreach ($repaylist as $repaym){
                    $d1=strtotime($repaym['repaydate']);
                    $d2=strtotime(date("Y-m-d").' 00:00:00');
                    $Days=round(($d2-$d1)/3600/24);
                    $credFlow = M('CredFlow')->field('cred_code,is_mos,money')->find('id='.$repaym['cred_flowid']);
                    if($Days>30){
                       //进行域名没收操作,2表示已经对域名进行了操作
                       if($credFlow['is_mos']==1){
                          $domanList = M('CredflowDomian')->field('domainid')->where("cred_code='".$credFlow['cred_code']."'")->select();
                          foreach ($domanList as $dom){
                              M('UserDomains')->where('id='.$dom['domainid'])->setField(array('trans_status'=>4));
                          }
                       }
                    }
                    /**
                     * 计算罚息，并修改借款流水为逾期中
                     */
                    M('RepaymentFlow')->where('id='.$repaym['id'])->setField(array('is_yq'=>2));
                    //借款金额*2%/30天*逾期天数*1.4倍
                    $penalty=$credFlow['money']*0.02;
                    $penalty=$penalty/30;
                    $penalty=$penalty*$Days*1.4;
                    if($Days>30 && $credFlow['is_mos']==1){
                        M('CredFlow')->where('id='.$repaym['cred_flowid'])->setField(array('status'=>3,'penalty'=>$penalty,'is_mos'=>2));
                    }else{
                        M('CredFlow')->where('id='.$repaym['cred_flowid'])->setField(array('status'=>3,'penalty'=>$penalty));
                    }
                }
            }
            $start=$start+100;
        }
        exit();
    }
    
    
    
}

?>